package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.WoplusServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;



@ChooseProvider(channel = "woplus")
public class WoplusAdaptor implements IAdaptor {

	private static Logger log = Logger.getLogger(WoplusAdaptor.class);

	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		log.info("bsm- 联通WO订单WoplusService.order() 订购请求productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = WoplusServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);
		String code = json.getString("ResultCode");
		
		if ("0".equals(code)) {
			
			orderBean.setMessage(json.getString("ResultDesc"));
			orderBean.setErrorMsg(json.getString("ResultDesc"));
			orderBean.setStatus("11");
		} else {
			
			orderBean.setMessage(json.getString("ResultDesc"));
			orderBean.setErrorMsg(json.getString("ResultDesc"));
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		
		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo = orderBean.getIds();
		log.info("bsm -联通WO状态WoplusService.orderStatus() 查询请求cstmOrderNo:" + cstmOrderNo);
		String result = WoplusServiceImpl.service.orderStatus(cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("ResultCode");

		if ("0".equals(code)) {
			orderBean.setMessage(json.getString("ResultDesc"));
			orderBean.setErrorMsg(json.getString("ResultDesc"));
			orderBean.setStatus("7");
		}else if ("9".equals(code)||"11".equals(code)||"1".equals(code)) {
			orderBean.setMessage(json.getString("ResultDesc"));
			orderBean.setErrorMsg(json.getString("ResultDesc"));
			orderBean.setStatus("1");
		}else {
			orderBean.setMessage(json.getString("ResultDesc"));
			orderBean.setErrorMsg(json.getString("ResultDesc"));
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
